﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using PWMIS.DataMap.Entity;
using PWMIS.DataProvider.Adapter;

namespace ConsoleApplication2.Dao
{
    /// <summary>
    /// 数据访问的综合帮助类
    /// </summary>
    public class MyDataHelp
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int Insert<T>(T t) where T : EntityBase, new()
        {
            return EntityQuery<T>.Instance.Insert(t);
        }

        public static int Insert<T>(List<T> ls) where T : EntityBase, new()
        {
            return EntityQuery<T>.Instance.Insert(ls);
        }

        public static int Delete<T>(T t) where T : EntityBase, new()
        {
            return EntityQuery<T>.Instance.Delete(t);
        }

        public static int Update<T>(T t) where T : EntityBase, new()
        {
            return EntityQuery<T>.Instance.Update(t);
        }

        public static int Update<T>(OQL oql) where T : EntityBase, new()
        {
            return EntityQuery<T>.Instance.ExecuteOql(oql);
        }



        public static List<T> FillList<T>(OQL2 oqlWhere) where T : EntityBase, new()
        {
            T t = new T();
            OQL oql = OQL.From(t).Select().Where(oqlWhere).END;
            return EntityQuery<T>.QueryList(oql);


        }

        public static List<T> FillList<T>() where T : EntityBase, new()
        {
            T t = new T();
            OQL oql = OQL.From(t).Select().END;
            return EntityQuery<T>.QueryList(oql);
        }


        public static List<T> FillListByOQl<T>(OQL oql) where T : EntityBase, new()
        {
            T t = new T();

            return EntityQuery<T>.QueryList(oql);
        }
        public static T FillObject<T>(T t) where T : EntityBase, new()
        {
            EntityQuery<T>.Instance.FillEntity(t);
            return t;
        }

        public static T FillObject<T>(OQL oql) where T : EntityBase, new()
        {
            T t = EntityQuery<T>.QueryObject(oql);
            return t;
        }



        /// <summary>
        /// 判断此对象是不是存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsExit<T>(T t) where T : EntityBase, new()
        {

            return EntityQuery<T>.Instance.ExistsEntity(t);
        }

        public static bool IsExit<T>(OQL oql) where T : EntityBase, new()
        {
            List<T> ls = FillListByOQl<T>(oql);
            return ls.Count > 0 ? true : false;
        }


        public static DataTable GetDataTableBySQL(string sql)
        {
            return MyDB.Instance.ExecuteDataSet(sql).Tables[0];
        }

        public static int SQL(string sql)
        {
            return MyDB.Instance.ExecuteNonQuery(sql);
        }

        public static DataSet GetDataSetBySQL(string sql)
        {
            return MyDB.Instance.ExecuteDataSet(sql);
        }
      



        public static DataTable SendSql(string sql)
        {
            return MyDB.Instance.ExecuteDataSet(sql).Tables[0];

        }

        public static int ExecuteScalar(string sql)
        {
            return Convert.ToInt32(MyDB.Instance.ExecuteScalar(sql));
        }

        public static T Scalar<T>(string sql)
        {
            return (T)MyDB.Instance.ExecuteScalar(sql);
        }


        public static bool Update(OQL oql)
        {
            PWMIS.DataProvider.Data.AdoHelper adoh = MyDB.Instance;
            return EntityQuery.ExecuteOql(oql, adoh) == 1 ? true : false;
        }

        /// <summary>
        /// 执行多条SQL
        /// </summary>
        /// <param name="str"></param>
        public static void SendSql(string[] str)
        {
            using (SqlConnection con = new SqlConnection(MyDB.Instance.ConnectionString))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand())
                {
                    com.Connection = con;
                    foreach (var item in str)
                    {
                        com.CommandText = item.ToString();
                        com.ExecuteNonQuery();
                    }
                }
            }
        }



        /// <summary>
        /// 执行多条SQL事物
        /// </summary>
        /// <param name="str"></param>
        public static void SendSqlByTransaction(string[] str)
        {

            using (SqlConnection con = new SqlConnection(MyDB.GetDBHelperByConnectionName("DataAccess.Properties.Settings.topLogConnectionString").ConnectionString))
            {

                con.Open();
                using (SqlCommand com = new SqlCommand())
                {
                    SqlTransaction tra = con.BeginTransaction();

                    com.Connection = con;
                    com.Transaction = tra;
                    try
                    {
                        foreach (var item in str)
                        {

                            com.CommandText = item.ToString();
                            com.ExecuteNonQuery();
                        }
                        tra.Commit();
                    }
                    catch (Exception)
                    {
                        tra.Rollback();
                        throw;
                    }

                }
            }
        }

    }
}
